<?php

namespace App\Models\ModelFilters;

use EloquentFilter\ModelFilter;

class OrdersFilter extends ModelFilter
{
    public $relations = [];

    public function goodsCode($goodsCode)
    {
        return $this->whereLike('goods.goods_code', $goodsCode);
    }

    public function goodsName($goodsName)
    {
        return $this->whereLike('goods.goods_name', $goodsName);
    }
	public function machinesName($machinesName)
    {
        return $this->whereLike('orders.machines_name', $machinesName);
        
    }

    public function adminName($adminName)
    {
        return $this->where('admin_users.name', $adminName);
        
    }
    
    public function categoryId($categoryId)
    {
        return $this->where('goods.category_id', $categoryId);
    }

    public function orderSn($orderSn)
    {
        return $this->whereLike('orders.ordersn', $orderSn);
    }
	
    public function machinesImel($machinesImel)
    {
        return $this->whereLike('orders.machines_imel', $machinesImel);
    }

    public function orderState($orderState)
    {
        $where = [];
        if($orderState == 2){
            $where[] = ['orders.order_status','=',$orderState];
        }
        if($orderState == 1){
            $where[] = ['orders.order_status','!=',2];
        }
        if($orderState == 99){
            $where[] = ['orders.refund_status','=',2];
        }
        return $this->where($where);
    }

    public function payment($payment)
    {
    	$where = [];
        if($payment == 2){
            $where[] = ['orders.payment','=',$payment];
        }else{
            $where[] = ['orders.payment','!=',2];
        }
        return $this->where($where);
    }

    public function refundStatus($refundStatus)
    {
        $where = [];
        if($refundStatus == 2){
            $where[] = ['orders.refund_status','=',$refundStatus];
        }else{
            $where[] = ['orders.refund_status','!=',2]; 
        }
        return $this->where($where);
    }
    
    public function paymentStart($payment_start)
    {
        if($payment_start != ''){
            return $this->whereDate('payment_time','>=',$payment_start);
        }
    }

    public function paymentEnd($payment_end)
    {
        if($payment_end != ''){
            return $this->whereDate('payment_time','<=',$payment_end);
        }
    }

    public function startTime($startTime){
        return $this->whereDate('orders.created_at','>=', $startTime);
    }

    public function endTime($endTime){
        return $this->whereDate('orders.created_at','<=', $endTime);
    }

}

